package com.rhb.erp.hr.person.web.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;
import java.text.*;
import org.apache.struts.action.*;

import com.rhb.erp.hr.person.bean.*;
import com.rhb.erp.hr.person.web.struts.form.PersonForm;
import com.rhb.erp.web.struts.action.ErpBaseDispatchAction;


public class PersonControlAction extends ErpBaseDispatchAction {
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		//System.out.println("unspecified for defalut parameter!");
		return list(mapping,form,request,response);
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		PersonForm eForm = (PersonForm)form;

		int allcount = this.getPersonBusiness().getAllCount_findByQuery(eForm.getPersonQuery());
		List list = null;
		if ( allcount != 0) {
			list = this.getPersonBusiness().findByQuery(eForm.getPersonQuery(),eForm.getStart(),eForm.getCount());
		}else{
			list = (List)new ArrayList();
		}

		eForm.setAllCount(allcount);
		eForm.setList(list);
		return mapping.findForward("list");
	}

	public ActionForward json(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		System.out.println("do json......");
		
		PersonForm oform = (PersonForm)form;
		String no = request.getParameter("no");
		try{
			if(no==null||"".equals(no)) no = request.getParameter("personNo");
			no = new String(no.getBytes("ISO8859_1"));
		}catch(Exception e){}
		if(no!=null&&!"".equals(no)) oform.getPerson().setPersonNo(no);

		oform.setPerson(this.getPersonBusiness().findByPersonNo(oform.getPerson().getPersonNo()));
		//oform.setDepartment(this.getDepartmentBusiness().findByPk(oform.getPerson().getDepartmentPk()));
		return mapping.findForward("json");
	}

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		PersonForm oform = (PersonForm)form;
		oform.setPerson(new Person());

		return mapping.findForward("add");
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		PersonForm oform = (PersonForm)form;
		oform.setPerson(this.getPersonBusiness().findByPk(oform.getPerson().getPersonPk()));
		return mapping.findForward("edit");
	}
	
	public ActionForward addFail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		PersonForm oform = (PersonForm)form;
		oform.setBehavior("add");
		return mapping.findForward("add");
	}
	
	public ActionForward editFail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		PersonForm oform = (PersonForm)form;
		oform.setBehavior("edit");
		return mapping.findForward("edit");
	}
	
	public ActionForward view(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		PersonForm oform = (PersonForm)form;
		oform.setPerson(this.getPersonBusiness().findByPk(oform.getPerson().getPersonPk()));
		return mapping.findForward("view");
	}
	
	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		return mapping.findForward("del");
	}
}
